package com.mdms.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import com.mdms.init.GetDBResourcesCommand;
import com.mdms.logger.SimplyMDLogger;


public class MDmsRessources extends ResourceBundle {

	private static SimplyMDLogger logger = new SimplyMDLogger(MDmsRessources.class);

	private String RESSOURCE_KEY="resources";

	private static boolean dbloaded=false;

	/** Mapa zasobow plikowych **/
	private static HashMap<String, ResourceBundle> mDMSResources = new HashMap<String, ResourceBundle>();
	private static HashMap<String, HashMap<String, String>> DBResources = new HashMap<String, HashMap<String, String>>();

	public MDmsRessources(){
		super();
		loadDBRessources();
		loadRessources(FacesUtils.getDefLocale());
	}


	public Locale getLocale(){
		return new Locale(FacesUtils.getDefLocale());

	}


	public void refresh(){
		dbloaded=false;
		loadDBRessources();
		loadRessources(FacesUtils.getDefLocale());
	}


	@Override
	public Enumeration<String> getKeys() {

		return getKeyz(FacesUtils.getDefLocale());
	}

	@Override
	protected Object handleGetObject(String key) {
		return getResource(key,FacesUtils.getDefLocale());
	}

	private static void loadDBRessources(){
		logger.debug("loadDBResources","START");
		if(!dbloaded){
			GetDBResourcesCommand command = new GetDBResourcesCommand();
			try{
				command.execute();
				if(command.getErrorCode()==0 && command.getResources()!=null
						&& command.getResources().size()>0){
					dbloaded=true;
					DBResources=command.getResources();
				}
			}catch(Exception e){
				logger.error("loadDBResources", e);
			}
		}


	}

	private void loadRessources(String lang){//Narazie tylko z pliku pozniej rozniwz z db
		lang = lang.toLowerCase();
		ResourceBundle resource = null;
		ClassLoader ldr = Thread.currentThread().getContextClassLoader();
		try {
			resource = ResourceBundle.getBundle(RESSOURCE_KEY, new Locale(lang), ldr);
		} catch (MissingResourceException mre) {
			logger.debug("loadProductBundle", "Product specific text resources not found");
			resource = null;
		}
		mDMSResources.put(lang, resource);

	}

	private Object getResource(String key,String lang){
		lang = lang.toLowerCase();
		Object value = null;
		if (getResources(lang) != null) {
			value = getResources(lang).get(key);
		}
		if(value==null){
			try{
				value = getMdmsResources(lang).getObject(key);
			}catch(MissingResourceException e){
				value = null;
			}
		}
		return value;
	}

	private Enumeration<String> getKeyz(String lang){
		List<String> list = new ArrayList<String>();
		if (getResources(lang) != null) {
			list.addAll(getResources(lang).keySet());
		}
		list.addAll(getMdmsResources(lang).keySet());
		return Collections.enumeration(list);
	}

	private static HashMap<String, String> getResources(String lang) {
		if (!dbloaded) {
			loadDBRessources();
		}
		return DBResources != null ?  DBResources.get(lang) : null;
	}

	private static ResourceBundle getMdmsResources(String lang) {
		lang = lang.toLowerCase();
		ResourceBundle resource = null;
		if (mDMSResources != null && mDMSResources.containsKey(lang)) {
			resource = mDMSResources.get(lang);
		}
		return resource;
	}


}
